/************************************************************************/
/*																		*/
/*		School Hangover Project											*/
/*																		*/
/*		IMAC 2010														*/
/*		Author : 	Helena Duchaussoy									*/
/*					Isabelle Gouwy										*/
/*					Nathalie Kuoch										*/
/*					Franck Letellier									*/
/*					Louise Pequignot									*/
/*																		*/
/*		E-mail: flo-in-underware@googlegroups.com						*/
/*																		*/
/************************************************************************/

#include "StageManager.h"
#include "AbstractStage.h"

#include "Stage.h"

#include <assert.h>

StageManager::StageManager(){

	m_pcurrentStage=NULL;
}

void StageManager::init(){

	///First we load all our stages
	AbstractStage* pstage = new Stage();
	loadStage(pstage);
	m_pcurrentStage = pstage;

	// Init the firstStage
	m_pcurrentStage->init();
}

void StageManager::clear(){
	
	for (std::map<std::string,AbstractStage*>::iterator iter = m_mstages.begin(); iter != m_mstages.end() ; ++iter){
		delete (*iter).second;
	}
	m_mstages.clear();

	m_pcurrentStage=NULL;
}

bool StageManager::loadStage(AbstractStage* pstage){

	assert(pstage);
	if (m_mstages.find(pstage->getName()) != m_mstages.end() ){
		std::cerr<<"ERROR : Stage is already added!"<<std::endl;
	}
	else{
		m_mstages[pstage->getName()] = pstage;
	}
	return true;
}

AbstractStage* StageManager::getStage(const std::string& sname){

	std::map<std::string,AbstractStage*>::iterator iter = m_mstages.find(sname);
	if (iter != m_mstages.end()){
		return (*iter).second;
	
	}
	else{
		std::cerr<<"ERROR : Stage has not been found"<<std::endl;
		return NULL;
	}
	

}

void StageManager::update(){
	m_pcurrentStage->update();
}

void StageManager::setCurrentStage(const std::string& sname){
	m_pcurrentStage = getStage(sname);
	m_pcurrentStage->init();
}

bool StageManager::isFinished(){
	return m_pcurrentStage->isFinished();
}

osg::Group* StageManager::getRootNode(){
	return m_pcurrentStage->getRootNode();
}